package queuedemo;

import java.util.Stack;

public class MyQueue1 {

    //用栈实现队列
    //两个栈
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;

    public MyQueue1() {
        this.stack1 = new Stack<>();
        this.stack2 = new Stack<>();
    }

    /**
     * 入队列
     *
     * @param x
     */
    public void push(int x) {
        stack1.push(x);
    }

    /**
     * 出队列
     *
     * @return
     */
    public int pop() {
        if (empty()) {
            return -1;
        }
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
        return stack2.pop();
    }

    /**
     * 队头元素
     *
     * @return
     */
    public int peek() {
        if (empty()) {
            return -1;
        }
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}

